package com.wbcs.system.component.application.report.configbean.editablereport;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.Config;
import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.config.database.type.AbsDatabaseType;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.assistant.WbcsAssistant;

public class DeleteSqlActionBean extends AbsEditSqlActionBean
{
    private static Log log=LogFactory.getLog(DeleteSqlActionBean.class);

    public DeleteSqlActionBean(EditActionGroupBean ownerGroupBean)
    {
        super(ownerGroupBean);
    }

    @Override
	public void parseActionscript(String reportTypeKey,String actionscript)
    {
        actionscript=this.parseAndRemoveReturnParamname(actionscript);
        this.lstParamBeans=new ArrayList<EditableReportParamBean>();
        this.sql=this.ownerGroupBean.getOwnerUpdateBean().parseStandardEditSql(actionscript,lstParamBeans,reportTypeKey);
        this.ownerGroupBean.addActionBean(this);
    }

    @Override
	public void updateData(ReportRequest rrequest,ReportBean rbean,Map<String,String> mRowData,Map<String,String> mParamValues)
            throws SQLException
    {
        PreparedStatement pstmt=null;
        AbsDatabaseType dbtype=rrequest.getDbType(this.ownerGroupBean.getDatasource());
        Connection conn=rrequest.getConnection(this.ownerGroupBean.getDatasource());
        try
        {
            if(Config.show_sql)
            {
                log.info("Execute sql:"+sql);
            }
            pstmt=conn.prepareStatement(sql);
            if(lstParamBeans!=null&&lstParamBeans.size()>0)
            {
                EditableReportParamBean paramBean;
                for(int j=0;j<lstParamBeans.size();j++)
                {
                    paramBean=lstParamBeans.get(j);
                    paramBean.getDataTypeObj().setPreparedStatementValue(j+1,
                            getParamValue(mRowData,mParamValues,rbean,rrequest,paramBean),pstmt,dbtype);
                }
            }
            int rtnVal=pstmt.executeUpdate();
            storeReturnValue(rrequest,mParamValues,String.valueOf(rtnVal));
        }finally
        {
            WbcsAssistant.getInstance().release(null,pstmt);
        }
    }
}
